package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.RankInfo;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RankAdapter extends RecyclerView.Adapter<RankAdapter.ViewHolder> {
    private ArrayList<RankInfo> mGrabRedDatas;
    private Context mContext;
    private int type;


    public RankAdapter(Context context, ArrayList<RankInfo> mGrabRedDatas, int type) {

        this.mGrabRedDatas = mGrabRedDatas;
        this.mContext = context;
        this.type = type;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rank, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (position == 0) {
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.drawable.ic_rank_1);
            holder.mTvRank.setVisibility(View.GONE);
        } else if (position == 1) {
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.drawable.ic_rank_2);
            holder.mTvRank.setVisibility(View.GONE);
        } else if (position == 2) {
            holder.mIvRank.setVisibility(View.VISIBLE);
            holder.mIvRank.setImageResource(R.drawable.ic_rank_3);
            holder.mTvRank.setVisibility(View.GONE);
        } else {
            holder.mIvRank.setVisibility(View.INVISIBLE);
            holder.mTvRank.setText(String.format("%d", position + 1));
            holder.mTvRank.setVisibility(View.VISIBLE);
        }
        RankInfo grabRedData = mGrabRedDatas.get(position);

        holder.mTvName.setText(grabRedData.getNickname());

        if (type == 0) {
            holder.mTvMoney.setText("已发红包:" + grabRedData.getPost_money() + "元");
            holder.mTvAmount.setText(grabRedData.getPost_sum() + "个");
        } else {
            holder.mTvMoney.setText("已抢红包:" + grabRedData.getGet_money() + "元");
            holder.mTvAmount.setText(grabRedData.getGet_sum() + "个");
        }

        if (grabRedData.getIs_users_agent() == 1) {
            holder.mIvVip.setVisibility(View.VISIBLE);
        } else {
            holder.mIvVip.setVisibility(View.GONE);
        }


        Glide
                .with(mContext)
                .load(grabRedData.getHeader())
                .error(R.drawable.ic_head)
                .placeholder(R.drawable.ic_head)
                .dontAnimate()
                .into(holder.mCivHead);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mGrabRedDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_head)
        CircleImageView mCivHead;

        @BindView(R.id.iv_vip)
        ImageView mIvVip;

        @BindView(R.id.iv_rank)
        ImageView mIvRank;

        @BindView(R.id.tv_name)
        TextView mTvName;
        @BindView(R.id.tv_money)
        TextView mTvMoney;
        @BindView(R.id.tv_amount)
        TextView mTvAmount;
        @BindView(R.id.tv_rank)
        TextView mTvRank;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


}